﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;


namespace GUI
{
    public partial class frmLoaiDocGia : DevExpress.XtraEditors.XtraForm
    {
        #region Khai Báo biến

        LoaiDocGia_BLL ldg_bll = new LoaiDocGia_BLL();
        TextMessageBox box = new TextMessageBox();
        public string maLoaiDocGia = "";
        DataTable dt;
        private bool isOK = false;
        #endregion

        #region LoadForm
        public frmLoaiDocGia()
        {
            InitializeComponent();
        }

        private void frmLoaiDocGia_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Các chức năng
        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }
        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }
        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region các sự kiện hệ thống
        private void frmLoaiDocGia_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        #endregion

        #region Hàm viết thêm
        private void UpdateData()
        {
            if (checkValid())
            {
                LoaiDocGia_Public ldg_public = new LoaiDocGia_Public();
                if (maLoaiDocGia == "")
                {
                    if (checkTenInsert())
                    {
                        ldg_public.MaLoaiDG = box.AutoID("LDG", 3, ldg_bll.LayDuLieu());
                        ldg_public.TenLoaiDG =box.toUpper(txtTenLoai.Text);

                        ldg_bll.Insert(ldg_public, ldg_public);
                        CloseForm();
                    }
                }
                else
                {
                    if (checkTenUpdate())
                    {
                        ldg_public.MaLoaiDG = txtMaLoai.Text.Trim();
                        ldg_public.TenLoaiDG =box.toUpper(txtTenLoai.Text);

                        ldg_bll.Update(ldg_public, ldg_public);
                        CloseForm();
                    }
                }
            }
        }
        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maLoaiDocGia != "")
            {
                dt = ldg_bll.TaoBang(box.sqlCheckExist("DM_LoaiDocGia", "MaLoaiDG", maLoaiDocGia));
                DataRow dr = dt.Rows[0];
                txtMaLoai.Text = dr["MaLoaiDG"].ToString();
                txtTenLoai.Text = dr["TenLoaiDG"].ToString();
            }
        }
        private bool checkValid()
        {
            //if (txtMaLoai.Text.Trim() == "")
            //{
            //    XtraMessageBox.Show("Bạn chưa nhập Mã loại độc giả.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    txtMaLoai.Focus();
            //    return false;
            //}
            if (txtTenLoai.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên loại độc giả.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTenLoai.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = ldg_bll.TaoBang(box.sqlCheckExist("DM_LoaiDocGia", "TenLoaiDG", txtTenLoai.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenLoai.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenLoai.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenUpdate()
        {
            dt = ldg_bll.TaoBang(box.sqlCheckExist("DM_LoaiDocGia", "TenLoaiDG", txtTenLoai.Text.Trim()));
            if (dt.Rows.Count+1 > 1)
            {
                XtraMessageBox.Show(box.Exist(txtTenLoai.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenLoai.Focus();
                return false;
            }
            return true;
        }
        private void SetInterface()
        {
            txtMaLoai.Text = "Mã tự sinh";
            txtTenLoai.Text = "";
            maLoaiDocGia = "";
            
            txtTenLoai.Focus();
        }
        #endregion


    }
}